yjx.service('sendCommonMsg', ['$rootScope', 'messageHandler', '$timeout', function ($rootScope, messageHandler, $timeout) {

    /*客服id*/
    var selType = webim.SESSION_TYPE.C2C;
    return {
        //发送消息(文本或者表情)
        onSendMsg: function (msgContent) {
            var selToID = $rootScope.service? $rootScope.service.userId:0;
            /*判断消息的合法性*/
            if (!selToID) {
                //alert("你还没有选中好友或者群组，暂不能聊天");
                return;
            }
            //获取消息内容
            var msgLen = webim.Tool.getStrBytes(msgContent);

            var maxLen, errInfo;
            if (selType == webim.SESSION_TYPE.C2C) {
                maxLen = webim.MSG_MAX_LENGTH.C2C;
                errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
            } else {
                maxLen = webim.MSG_MAX_LENGTH.GROUP;
                errInfo = "消息长度超出限制(最多" + Math.round(maxLen / 3) + "汉字)";
            }
            if (msgLen > maxLen) {
                //alert(errInfo);
                return;
            }
            //发消息处理
            this.handleMsgSend(msgContent);
        },

        handleMsgSend: function (msgContent) {
            /*
             * selType：会话类型， 包括群聊和私聊，具体参考webim. SESSION_TYPE常量对象，必填
             * selToID： 对方id , 群聊时，为群id；私聊时，对方帐号，必填
             * 其他都选填
             * */
            var selToID = $rootScope.service? $rootScope.service.userId:0;
            if (!selSess) {
                var selSess = new webim.Session(selType, selToID);
            }
            var isSend = true; //是否为自己发送
            var seq = -1; //消息序列，-1表示sdk自动生成，用于去重
            var random = Math.round(Math.random() * 4294967296); //消息随机数，用于去重
            var msgTime = Math.round(new Date().getTime() / 1000); //消息时间戳
            var subType; //消息子类型
            if (selType == webim.SESSION_TYPE.C2C) {
                subType = webim.C2C_MSG_SUB_TYPE.COMMON;
            } else {
                //webim.GROUP_MSG_SUB_TYPE.COMMON-普通消息,
                //webim.GROUP_MSG_SUB_TYPE.LOVEMSG-点赞消息，优先级最低
                //webim.GROUP_MSG_SUB_TYPE.TIP-提示消息(不支持发送，用于区分群消息子类型)，
                //webim.GROUP_MSG_SUB_TYPE.REDPACKET-红包消息，优先级最高
                subType = webim.GROUP_MSG_SUB_TYPE.COMMON;
            }
            /*
             selSess:消息所属的会话(e.g:我与好友A的C2C会话，我与群组G的GROUP会话)
             isSend:消息是否为自己发送标志:true：表示是我发出消息,false：表示是发给我的消息
             loginInfo.identifier :	消息发送者帐号
             * */
            var msg = new webim.Msg(selSess, isSend, seq, random, msgTime, $rootScope.loginInfo.identifier, subType, $rootScope.loginInfo.identifierNick);

            var text_obj, face_obj, tmsg, emotionIndex, emotion, restMsgIndex;
            //解析文本和表情
            var expr = /\[[^[\]]{1,3}\]/mg;
            var emotions = msgContent.match(expr);
            if (!emotions || emotions.length < 1) {
                // console.log("他说发出去了文本消息=============================")
                text_obj = new webim.Msg.Elem.Text(msgContent);
                msg.addText(text_obj);
            }
            else {
                for (var i = 0; i < emotions.length; i++) {
                    tmsg = msgContent.substring(0, msgContent.indexOf(emotions[i]));
                    if (tmsg) {
                        text_obj = new webim.Msg.Elem.Text(tmsg);
                        msg.addText(text_obj);
                    }
                    emotionIndex = webim.EmotionDataIndexs[emotions[i]];
                    emotion = webim.Emotions[emotionIndex];

                    if (emotion) {
                        face_obj = new webim.Msg.Elem.Face(emotionIndex, emotions[i]);
                        msg.addFace(face_obj);
                    } else {
                        text_obj = new webim.Msg.Elem.Text(emotions[i]);
                        msg.addText(text_obj);
                    }
                    restMsgIndex = msgContent.indexOf(emotions[i]) + emotions[i].length;
                    msgContent = msgContent.substring(restMsgIndex);
                }
                if (msgContent) {
                    text_obj = new webim.Msg.Elem.Text(msgContent);
                    msg.addText(text_obj);
                }
            }

            msg.PushInfo = {};
            msg.PushInfoBoolean = true; //是否开启离线推送push同步
            msg.sending = 1;
            msg.originContent = msgContent;
            msg.fromAccountHeadurl = $rootScope.user?$rootScope.user.avatar:'';
            //this.addMsg(msg);
            //angular.element(document.getElementById("send_msg_text")).html('');
            //turnoffFaces_box();

            webim.sendMsg(msg, function (resp) {
                //发送成功，把sending清理
                //$("#id_" + msg.random).find(".spinner").remove();
                $timeout(function () {
                    addMsg(msg.originContent);
                })
            }, function (err) {
                //提示重发
                //showReSend(msg);
            });
            function addMsg(msgContent) {
                var message = {
                    from: $rootScope.userId,
                    text: msgContent,
                    to: $rootScope.service.userId,
                }
                messageHandler.addToMessageList(message, $rootScope.service)
                /*更新messageList*/
                messageHandler.addToLocal(message, $rootScope.service);
                /*更新本地存储*/
                if (!$rootScope.messageListShow.length > 0) {
                    //$rootScope.messageListShow.push($scope.message);
                    $rootScope.messageListShow = messageHandler.getCurMessageAllContent($rootScope.messageList, $rootScope.service.userId);//当前窗口是空的时候从messagelist中获取消息对象
                }
            }
        },


    }
}])